package com.zoe.onelink.logging.trace.dubbo;

import brave.SpanCustomizer;
import brave.propagation.TraceContext;
import brave.rpc.RpcRequest;
import brave.rpc.RpcRequestParser;
import brave.rpc.RpcTags;
import com.zoe.onelink.util.StrUtil;

/**
 * <p>标题: </p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2022</p>
 * <p>公司: 智业软件股份有限公司</p>
 *
 * @version: 1.0
 * @author: ruansheng
 * @date: 2022-05-27
 */
public class DubboTraceRequestParser implements RpcRequestParser {
    @Override
    public void parse(RpcRequest request, TraceContext context, SpanCustomizer span) {
        String service = request.service();
        String method = request.method();
        if (service == null && method == null) {
            return;
        }
        if (service == null) {
            span.tag(RpcTags.METHOD.key(), method);
            span.name(method);
        } else if (method == null) {
            span.tag(RpcTags.SERVICE.key(), service);
            span.name("[DUBBO] " + service);
        } else {
            span.tag(RpcTags.SERVICE.key(), service);
            span.tag(RpcTags.METHOD.key(), method);
            String spanName = StrUtil.format("[DUBBO] {}.{}", service, method);
            span.name(spanName);
        }
    }
}
